#include "../HDCTreeWidget.h"
#include "../../include/StdAfx.h"
#include "../../include/HGlobalVariable.h"

HDCTreeWidget::HDCTreeWidget(QWidget *parent)
    :QTreeWidget(parent)
{
	m_lstLeafItems.clear();
	m_lstTotalItems.clear();
	m_hashItemToInfo.clear();
    this->header()->hide();
	this->setSortingEnabled(true);
	this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	this->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	this->resizeColumnToContents(0);
    initUIStyle();
	connect(this,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(sltItemDoubleClicked(QTreeWidgetItem*,int)));
	connect(this,SIGNAL(itemExpanded(QTreeWidgetItem*)),this,SLOT(sltItemExpanded(QTreeWidgetItem*)));
}

HDCTreeWidget::~HDCTreeWidget()
{

}

void HDCTreeWidget::updateUI(QList<HTTreeItemInfo*> lstItemInfos)
{
	clearTreeWidget();
    createTreeItem(NULL,lstItemInfos);
	QTreeWidgetItem *item = this->topLevelItem(0);
	this->expandItem(item);
	int nTopLevelChildCount = item->childCount();
	for(int childIndex = 0; childIndex < nTopLevelChildCount; childIndex ++)
	{
		this->collapseItem(item->child(childIndex));
	}
	this->resizeColumnToContents(0);
}

void HDCTreeWidget::appendTreeItem(HTTreeItemInfo* itemInfo)
{
	int treeItemInfoID = itemInfo->m_hashIDToData.value(VIDEO).toInt();

	for(int i = 0; i < m_lstTotalItems.count(); i ++)
	{
		QTreeWidgetItem *item = m_lstTotalItems.at(i);
		QVariant varItemData = item->data(0,101);
		QHash<QString,QVariant> hashItemData = varItemData.toHash();
		int nDeviceID = hashItemData.value(VIDEO).toInt();

		if(treeItemInfoID == nDeviceID)
		{
			HTTreeItemInfo *t_itemInfo = m_hashItemToInfo.value(item);
			bool bIsGroup = t_itemInfo->m_bIsGroup;
			if(bIsGroup)
			{
				createTreeItem(item,itemInfo->m_lstChildItems);
			}
		}
	}
}

HTTreeItemInfo HDCTreeWidget::getTreeItemInfo(QTreeWidgetItem* item)
{
	if(item)
	{
		return *(m_hashItemToInfo.value(item));
	}
}

QList<QTreeWidgetItem*> HDCTreeWidget::getAllTreeItems()
{
	return m_lstTotalItems;
}

void HDCTreeWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    QTreeWidget::paintEvent(event);
}

void HDCTreeWidget::initUIStyle()
{
    QFile file(g_strAppPath + QSSRELATIONPATH + "HDomeControlPanelTree.qss");
    if(file.open(QIODevice::ReadOnly))
    {
        QString strStyle = file.readAll();
        this->setStyleSheet(strStyle.arg(g_strAppPath + g_strImageRelationPath));
    }
}

void HDCTreeWidget::createTreeItem(QTreeWidgetItem *parentItem, QList<HTTreeItemInfo*> lstItemInfos)
{
    for(int i = 0; i < lstItemInfos.count(); i ++)
    {
        HTTreeItemInfo *itemInfo = lstItemInfos.at(i);
		//QList<QTreeWidgetItem*> lstChildItems = parentItem
		bool bHasOne = false;
		if(parentItem)
		{
			int nChildCount = parentItem->childCount();
			for(int childIndex = 0; childIndex < nChildCount; childIndex++)
			{
				QTreeWidgetItem *childItem = parentItem->child(childIndex);
				QHash<QString,QVariant> childItemData = childItem->data(0,101).toHash();
				QString nChildItemDiviceData = childItemData.value(VIDEO).toString();
				QString strDeviceID = nChildItemDiviceData.mid(0,nChildItemDiviceData.indexOf(","));
				QString deviceID = strDeviceID.remove(DIVICEID);

				if(deviceID.toInt() == itemInfo->m_hashIDToData.value(VIDEO).toInt())
				{
					bHasOne = true;
					break;
				}
			}
		}
		
		if(!bHasOne)
		{
			QTreeWidgetItem *item = new QTreeWidgetItem();
			m_hashItemToInfo.insert(item,itemInfo);
			m_lstTotalItems<<item;
			if(itemInfo->m_lstChildItems.count() > 0)
			{
				item->setIcon(0,QIcon(g_strAppPath + g_strImageRelationPath + "HIDI_TREE_GROUP.png"));
				createTreeItem(item,itemInfo->m_lstChildItems);
			}
			else
			{
				if(itemInfo->m_bIsGroup)
				{
					item->setIcon(0,QIcon(g_strAppPath + g_strImageRelationPath + "HIDI_TREE_GROUP.png"));
				}
				else
				{
					m_lstLeafItems.append(item);
					if(itemInfo->m_bOnline)
					{
						item->setIcon(0,QIcon(g_strAppPath + g_strImageRelationPath + "HIDI_TREE_ITEM_ONLINE.png"));
					}
					else
					{
						item->setIcon(0,QIcon(g_strAppPath + g_strImageRelationPath + "HIDI_TREE_ITEM_OFFLINE.png"));
					}
				}
			}

			item->setText(0,itemInfo->m_strItemText);
			//item->setData(0,101,itemInfo->m_hashIDToData.value(0).toInt());
			item->setData(0,101,QVariant(itemInfo->m_hashIDToData));

			if(parentItem == NULL)
			{
				this->addTopLevelItem(item);
			}
			else
			{
				parentItem->addChild(item);
			}
		}
    }
}

void HDCTreeWidget::deleteTreeItem(QTreeWidgetItem *parentItem)
{
	if(parentItem)
	{
		while(parentItem->childCount())
		{
			QTreeWidgetItem * childItem = parentItem->child(0);
			deleteTreeItem(childItem);
		}
		delete parentItem;
		parentItem = NULL;
	}
	
}

void HDCTreeWidget::sltItemDoubleClicked(QTreeWidgetItem *item,int nColumn)
{
	if(m_lstLeafItems.contains(item))
	{
		emit sigTreeLeafItemDoubleClicked(m_hashItemToInfo.value(item));
	}
	else
	{
		if(item->childCount() == 0)
		{
			emit sigTreeGroupItemDoubleClicked(m_hashItemToInfo.value(item));
		}
	}
}

void HDCTreeWidget::sltItemExpanded(QTreeWidgetItem *item)
{
	emit sigTreeGroupItemDoubleClicked(m_hashItemToInfo.value(item));
	this->resizeColumnToContents(0);
}

void HDCTreeWidget::clearTreeWidget()
{
	int nToplevelItemsCount = this->topLevelItemCount();
	while(this->topLevelItemCount())
	{
		QTreeWidgetItem *toplevelItem = this->takeTopLevelItem(0);
		deleteTreeItem(toplevelItem);
	}
	this->clear();
	m_lstLeafItems.clear();
	m_lstTotalItems.clear();
}
